package config;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author tuyou_cd
 * @date 2020/12/18 15:04
 */
public class RabbitConfig {
    public static String ip = "127.0.0.1";
    public static int port = 5672;
    public static String username = "guest";
    public static String password = "guest";

    static ConnectionFactory connectionFactory;
    static Connection connection;

    static {
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(ip);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
    }

    public static Connection getConnection() {
        try {
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static Channel createChannel() throws IOException {
        return getConnection().createChannel();
    }
}
